package com.atguigu.srb.core.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.atguigu.common.exception.BusinessAssert;
import com.atguigu.common.exception.BusinessException;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.dto.ExcelDictDTO;
import com.atguigu.srb.core.pojo.entity.Dict;
import com.atguigu.srb.core.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * @program: srb
 * @author: 龙龙
 * @create: 2022-01-24 10:43
 * @description:
 **/
@Api(value = "数据字典管理")
//@CrossOrigin
@RestController
@RequestMapping("/admin/core/dict/")
@Slf4j
public class AdminDictController {

    @Resource
    private DictService dictService;

    @ApiOperation("字典文件上传")
    @PostMapping("import")
    public R importFile(
            @ApiParam(value = "Excel文件", required = true)
            @RequestParam("file") MultipartFile multipartFile) {

//        try {
        boolean b = dictService.importData(multipartFile);
        //统一异常处理
        BusinessAssert.isTrue(b, ResponseEnum.UPLOAD_ERROR);
        return R.ok().message("批量导入成功");
//        } catch (Exception e) {
//            UPLOAD_ERROR(-103, "文件上传错误"),
//            throw new BusinessException(ResponseEnum.UPLOAD_ERROR, e);
//        }
    }

    @ApiOperation("字典文件导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        ServletOutputStream outputStream = null;
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("mydict", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<ExcelDictDTO> excelDictDTOS = dictService.listDictData();

        EasyExcel.write(outputStream, ExcelDictDTO.class).sheet("数据字典").doWrite(excelDictDTOS);

    }

    @ApiOperation("查询字典集合")
    @GetMapping("listByParentId/{parentId}")
    public R listByParentId(
            @ApiParam(value = "上级结点id", required = true)
            @PathVariable Long parentId) {
        List<Dict> dictList = dictService.listByParentId(parentId);
        return R.ok().data("list", dictList);
    }

}
